<?php
// +----------------------------------------------------------------------
// | 授信日志查询  
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/09/16
// +----------------------------------------------------------------------
include('/var/www/html/new/com.inc.php');

if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆过期","code":0}');
if(!isset($_REQUEST['act'])) exit('{"status":"fail","msg":"参数错误"}');



switch(trim($_REQUEST['act'])){
	
	// 分销商查看授信记录
	case 'Query_Record':
		// $disid = $_SESSION['sid'];
		$type  = (isset($_GET['type'])) ? $_GET['type']+0:0;// 0 分销商查询供应商 1 供应商商查询分销商
		$dtype = (isset($_GET['dtype']))? $_GET['dtype']+0:4;

        switch ($dtype) {
            case '4'://  预存/还款记录
            case '11':// 信用额度调整记录
                $where_dtype = " j.dtype=$dtype";
                break;
            case '1'://  预存/还款记录 + 产品交易记录
                $where_dtype =  "j.dtype in (0,1,4,17)";
                break;
            case '2'://产品交易记录
                $where_dtype =  "j.dtype in (0,1,17)";
                break;
            default:
                exit('{"status":"fail","msg":"参数错误"}');
        }

		if($type==0){
			$disid = $_SESSION['sid'];
			$supid = $_GET['id']+0;
		}
		if($type==1){
			$supid = $_SESSION['sid'];
			$disid = $_GET['id']+0;
		}

        if($_GET['loadExcel']==1){
            $pStart = 0;
            $pageSize = 9999;
        }else{
            $pageSize    = (isset($_GET['pageSize'])) ? $_GET['pageSize']+0:15;
            $currentPage = (isset($_GET['currentPage'])) ? $_GET['currentPage']+0:1;
            $pStart      = ($currentPage-1) * $pageSize;
        }

		$op = (isset($_GET['bt']) && $_GET['bt']) ? $_GET['bt']:date('Y-m-d', time()-2592000);// 30 day
		$ed = (isset($_GET['et']) && $_GET['et']) ? $_GET['et']:date('Y-m-d');
		

		// 获取数据
		$where = "where rectime between '".$op." 00:00:00' and '".$ed." 23:59:59' and $where_dtype and aid=$supid and j.fid=$disid";
		$sql = "select dname,rectime,j.dtype,daction,dmoney,lmoney,opid,memo,orderid from pft_member_journal j left join pft_member m on m.id=j.opid $where order by rectime desc,orderid desc limit $pStart,$pageSize";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $_note[] = FormatData($row);
		
		// 统计总数
		$sql = "select count(j.id) as c from pft_member_journal j left join pft_member m on m.id=j.opid $where";
		$GLOBALS['le']->query($sql);
		$GLOBALS['le']->fetch_assoc();
		$total = $GLOBALS['le']->f('c');
		$totalPage = ceil($total/$pageSize);
		if(isset($_GET['loadExcel']) && $_GET['loadExcel']==1) goto loadExcel;
		exit(json_encode(array('status'=>'success', 'list'=>$_note, 'pageSize'=>$pageSize, 'totalPage'=>$totalPage, 'currentPage'=>$currentPage)));
		break;
	default:exit('{"status":"fail","msg":"参数错误"}');break;
}


loadExcel:
include("/var/www/html/new/d/class/SimpleExcel.class.php");
$n = 0;
$Excel[0]['rectime']='日期';
$Excel[0]['dname']='操作员';
$Excel[0]['dreason']='操作说明';
$Excel[0]['add']='增加金额';
$Excel[0]['redu']='减少金额';
$Excel[0]['lmoney']='信用账户余额';
$Excel[0]['memo']='备注';
foreach($_note as $row){
	++$n;
	$Excel[$n]['rectime']=$row['rectime'];
	$Excel[$n]['dname']=$row['dname'];
	$Excel[$n]['dreason']=$row['dreason'];
	$Excel[$n]['add']=$row['add'];
	$Excel[$n]['redu']=$row['redu'];
    $Excel[$n]['lmoney']=$row['lmoney']/100;
    $Excel[$n]['memo']=$row['memo'];
}
$filename=date('YmdHis').'授信/预存';
$xls = new SimpleExcel('UTF-8', true,'授信/预存');
$xls->addArray($Excel);
$xls->generateXML($filename);
exit;




function FormatData($row){
	if($row['dtype']==4){
		if($row['daction']=="0"){
			$row['add_t']   = $row['dmoney']/100;
			$row['dreason'] = $row['opid']==$_SESSION['memberID']?"分销商还款":"线下预存";
		}else{
			$row['redu_t']  = $row['dmoney']/100;
			$row['dreason'] = "线下预存";
		}
	}
	if($row['dtype']==11){
		if($row['daction']=="0"){
			$row['add_c'] = $row['dmoney']/100;
		}else{
			$row['redu_c'] = $row['dmoney']/100;
		}
		$row['dreason']="供应商调整授信额度";
	}
    if($row['dtype']==0){
        $row['dreason']="下单";
    }
    if($row['dtype']==1){
        if($row['daction']==2)
            $row['dreason']="退款手续费";
        else
            $row['dreason']="撤改订单";
    }
    if($row['dtype']==17){
        $row['dreason']="撤销撤改";
    }
	if($row['daction']=="0"){
		$row['add']=$row['dmoney']/100;
	}
	else{
		$row['redu']=$row['dmoney']/100;
	}
    $row['dname'] = $row['dname'] ? $row['dname'] : '无';
    $row['memo']  = $row['memo']  ? $row['memo']  : ' ';

	return $row;
}